package com.example.demo;

/**
 * @Author: lz
 * @Date: 2018/8/21 16:24
 * @Version 1.0
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import reactor.ipc.netty.http.server.HttpServer;

@Configuration
public class HttpServerConfig {
	@Autowired
	private Environment environment;

	@Bean
	public HttpServer httpServer(RouterFunction<?> routerFunction) {
		HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction);
		ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
		HttpServer server = HttpServer.create("localhost", Integer.valueOf(environment.getProperty("server.port")));
		server.newHandler(adapter);

		return server;
	}
//	public void tmcat(){
//		Tomcat tomcatServer = new ServerProperties.Tomcat();
//		tomcatServer.setHostname("localhost");
//		tomcatServer.setPort(Integer.valueOf(environment.getProperty("server.port")));
//		Context rootContext = tomcatServer.addContext("", System.getProperty("java.io.tmpdir"));
//		ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
//		Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
//		rootContext.addServletMapping("/", "httpHandlerServlet");
//		tomcatServer.start();
//	}
}